'''
Created on Dec 6, 2009

@author: morgan
'''

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from model import User
from util import AuthorizedPage

class ProfilePage(AuthorizedPage):
    def getIfAuthorized(self):         
        username = self.request.get("user")
        
        user = User.getUserByUsername(username)
        
        project_list = []
        for proj in user.getProjects():
            project_list.append({"url":proj.url, "name":proj.name})
            
        auction_list = []
        for auction in user.getAuctions():
            auction_list.append({"url": auction.url, 
                                 "name":auction.project_ref.name + " Auction"})            
        
        entries_list = []
        for entry in user.getBlogEntries():
            entries_list.append({"title": entry.title,
                                 "date": entry.date,
                                 "content": entry.content,
                                 })
        
        templateData = {
                "name": user.name,
                "gender": user.gender,
                "age": user.age,
                "location": user.city,
                "website": user.link,
                "auctions": auction_list,
                "blogentries": entries_list,
                "projects": project_list,
                "my_page": user.username == self.authorizedUser.username,
            }
        
        
        self.outputHeader()
                
        templatePath = os.path.join(os.path.dirname(__file__), 'profile.html')
        self.response.out.write(template.render(templatePath, templateData)) 
        
            
